package org.ydsw.data.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ydsw.data.manage.entity.DealDictionaryEntity;
import org.ydsw.data.manage.mapper.DealDictionaryEntityMapper;
import org.ydsw.data.manage.service.DealDictionaryService;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


@Service
public class DealDictionaryServiceImpl implements DealDictionaryService {
    @Autowired
    private DealDictionaryEntityMapper dealDictionaryEntityMapper;

    @Override
    public int dealDictionaryData() {
        List<DealDictionaryEntity> dealDictionaryEntities = new DealDictionaryServiceImpl().parseCsv();
        int i = dealDictionaryEntityMapper.insertDictionarys(dealDictionaryEntities);
        return i;
    }

    @Override
    public List<DealDictionaryEntity> selectMetaData() {

        List<DealDictionaryEntity> dealDictionaryEntities = dealDictionaryEntityMapper.selectMetaData();
        return dealDictionaryEntities;
    }


    private List<DealDictionaryEntity> parseCsv() {
        BufferedReader reader = null;
        try {
            InputStreamReader read = new InputStreamReader(new FileInputStream("E:\\DataDictionary\\EasDataDictionary.csv"), "UTF-8");
            reader = new BufferedReader(read);
            String line = null;
            List<DealDictionaryEntity> dealDictionarys = new ArrayList<>();
            String tableEnName = "";
            String tableChName = "";
            while((line =reader.readLine()) != null){
//                line = reader.readLine();
                System.out.println(line);
//                System.out.println(line);
                DealDictionaryEntity dealDictionaryEntity = new DealDictionaryEntity();
                String[] split = line.split(",");
                System.out.println(split.length);
                if (split[0].contains("（") && line.contains("）")) {
                    tableEnName = line.substring(0, line.indexOf("（"));
                    tableChName = line.substring(line.indexOf("（"));
                    continue;
                }

                switch (split.length){
                    case 6:{
                        dealDictionaryEntity.setTableName(tableEnName);
                        dealDictionaryEntity.setTableChName(tableChName.substring(tableChName.indexOf("（")+1,tableChName.indexOf("）")));
                        dealDictionaryEntity.setFieldName(split[0]);
                        dealDictionaryEntity.setFieldSecondaryName(split[1]);
                        dealDictionaryEntity.setDataType(split[2]);
                        dealDictionaryEntity.setDataLength(split[3]);
                        dealDictionaryEntity.setDataAccuracy(split[4]);
                        dealDictionaryEntity.setDataDecimalCount(split[5]);
                        break;
                    }
                    case 7:{
                        dealDictionaryEntity.setTableName(tableEnName);
                        dealDictionaryEntity.setTableChName(tableChName.substring(tableChName.indexOf("（")+1,tableChName.indexOf("）")));
                        dealDictionaryEntity.setFieldName(split[0]);
                        dealDictionaryEntity.setFieldSecondaryName(split[1]);
                        dealDictionaryEntity.setDataType(split[2]);
                        dealDictionaryEntity.setDataLength(split[3]);
                        dealDictionaryEntity.setDataAccuracy(split[4]);
                        dealDictionaryEntity.setDataDecimalCount(split[5]);
                        dealDictionaryEntity.setDataTips(split[6]);
                        break;
                    }
                }
                dealDictionarys.add(dealDictionaryEntity);
            }
            dealDictionarys.forEach(System.out::println);
            return dealDictionarys;


        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
